// Copyright 2007, 2008 - Ariel Yaroshevich (a.k.a blackrussian) (hamlet@inter.net.il)
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 


using System;
using System.Collections.Generic;
using System.Text;

namespace SharpMap.Wpf.Rendering.Caching
{
    class SimpleLruCachePolicy<TKey, TValue> : CachePolicy<TKey,TValue>
        where TKey : IEquatable<TKey>
     {
        int _maxItems;
        int _minCapacity;
        Queue<TKey> _arrivalOrder;

        public SimpleLruCachePolicy(Cache<TKey,TValue> cache, int maxItems)
            :base(cache)
        {
            _maxItems = maxItems;
            _minCapacity = maxItems / 4;
            _arrivalOrder = new Queue<TKey>(_minCapacity);
        }
        public override Boolean UpperLimitReached
        {
            get {
                return _arrivalOrder.Count >= _maxItems;
            }
        }

        public override Boolean LowerLimitReached
        {
            get 
            {
                return _arrivalOrder.Count < _minCapacity;
            }
        }

        public override void RecordItem(TKey key)
        {
            _arrivalOrder.Enqueue(key);
        }
        public override void Remove(TKey key)
        {
            throw new NotImplementedException("Remove not implemented");
        }
        public override TKey Dequeue()
        {
            return _arrivalOrder.Dequeue();
        }
        public override void Clear()
        {
            _arrivalOrder.Clear();
        }
    }
}